﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace EntityFrameworks.Entity.Core
{
    /// <summary>
    /// 链接属性
    /// </summary>
    public class LinkDetailAttribute : Attribute
    {
        private string _link;
        /// <summary>
        /// 获链接取属性值，第一个参数为对象ID，第二个参数对象名称 例如： <![CDATA[ <a href='/User/ShowInfo?UserID={0}'>{1}</a>]]>
        /// </summary>
        public string LinkDetail
        {
            get
            {
                return _link;
            }
        }

        public LinkDetailAttribute(string Link)
        {
            _link = Link;
        }
    }
    /// <summary>
    /// LinkDetailAttribute属性扩展方法
    /// </summary>
    public static class EnumLinkDetail
    {

        /// <summary>
        /// 得到Enum类型的LinkDetail特性
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string GetLinkDetail(this Enum obj)
        {
            if (obj == null)
            {
                return string.Empty;
            }
            try
            {
                Type _enumType = obj.GetType();
                LinkDetailAttribute dna = null;
                FieldInfo fi = _enumType.GetField(Enum.GetName(_enumType, obj));
                dna = (LinkDetailAttribute)Attribute.GetCustomAttribute(fi, typeof(LinkDetailAttribute));
                if (dna != null)
                    return dna.LinkDetail;
            }
            catch
            {
                throw;
            }
            return string.Empty;
        }
    }
}
